<template>
  <view class="todo-container">
    <!-- 列表内容 -->
    <view class="todo-list">
      <view
        class="todo-item"
        v-for="(item, index) in todoList"
        :key="index"
        @tap="showDetail(item)"
      >
        <view class="todo-info">
          <view class="todo-name">{{ item.name }}</view>
          <view class="todo-time">{{ item.time }}</view>
        </view>
        <van-icon name="arrow" />
      </view>
    </view>

    <!-- 悬浮添加按钮 -->
    <view class="add-btn" @tap="showForm = true">
      <van-icon name="plus" size="24px" color="#fff" />
    </view>

    <view class="call-icon" @tap="makePhoneCall('04774217332')">
          <van-icon name="phone-o" size="24px" color="#fff" />
    </view>

    <!-- 新增表单弹窗 -->
    <van-popup
      v-model:show="showForm"
      position="bottom"
      round
      :style="{ height: '70%' }"
    >
      <view class="form-container">
        <view class="form-title">新增帮办代办</view>
        <van-form @submit="onSubmit">
          <van-field
            v-model="formData.name"
            name="name"
            label="名称"
            placeholder="请输入代办名称"
            :rules="[{ required: true, message: '请输入代办名称' }]"
          />
          <van-field
            v-model="formData.detail"
            name="detail"
            label="详细信息"
            type="textarea"
            placeholder="请输入详细信息"
            :rules="[{ required: true, message: '请输入详细信息' }]"
          />
          <van-field
            v-model="formData.personName"
            name="personName"
            label="姓名"
            type="input"
            placeholder="请输入姓名"
            :rules="[{ required: true, message: '请输入姓名' }]"
          />
          <van-field
            v-model="formData.personPhone"
            name="personPhone"
            label="联系方式"
            type="input"
            placeholder="请输入联系方式"
            :rules="[{ required: true, message: '请输入联系方式' }]"
          />
          <van-field
            v-model="formData.time"
            name="time"
            label="时间"
            placeholder="请选择时间"
            readonly
            :rules="[{ required: true, message: '请选择时间' }]"
            @tap="showTimePicker = true"
          />
          <view class="form-btns">
            <van-button round block type="primary" native-type="submit"
              >提交</van-button
            >
          </view>
        </van-form>
      </view>
    </van-popup>

    <!-- 时间选择器 -->
    <van-popup v-model:show="showTimePicker" position="bottom">
      <van-datetime-picker
        type="datetime"
        title="选择时间"
        :min-date="minDate"
        :max-date="maxDate"
        @confirm="onTimeConfirm"
        @cancel="showTimePicker = false"
      />
    </van-popup>

    <!-- 详情弹窗 -->
    <van-popup
      v-model:show="showDetailPopup"
      position="bottom"
      round
      :style="{ height: '60%' }"
    >
      <view class="detail-container">
        <view class="detail-header">
          <view class="detail-title">{{ currentDetail.name }}</view>
          <view class="detail-time">{{ currentDetail.time }}</view>
        </view>
        <view class="detail-content">
          <view class="detail-label">详细信息：</view>
          <view class="detail-text">{{ currentDetail.detail }}</view>
          <view class="detail-text">联系电话：{{ currentDetail.personPhone }}</view>
          <view class="detail-text">联系人：{{ currentDetail.personName }}</view>
        </view>
        <view class="detail-footer">
          <van-button round block type="primary" @tap="showDetailPopup = false"
            >关闭</van-button
          >
        </view>
      </view>
    </van-popup>
  </view>
</template>

<script>
export default {
  data() {
    return {
      todoList: [],
      showForm: false,
      showTimePicker: false,
      showDetailPopup: false,
      minDate: new Date(),
      maxDate: new Date(new Date().getFullYear() + 1, 11, 31),
      formData: {
        name: '',
        detail: '',
        time: '',
      },
      currentDetail: {
        name: '',
        time: '',
        detail: '',
      },
    }
  },
  onLoad() {
    this.getList()
  },
  methods: {
    // 获取列表数据
    async getList() {
      try {
        const res = await this.$http({
          url: '/tbTodo/list',
          method: 'post',
          data: {
            userId: this.$store.state.userInfo.userId,
          },
        })
        if (res.code === '0') {
          this.todoList = res.data || []
        }
      } catch (error) {
        uni.showToast({
          title: '网络错误',
          icon: 'none',
        })
      }
    },
    // 提交表单
    async onSubmit() {
      try {
        const res = await this.$http({
          url: '/tbTodo/save',
          method: 'post',
          data: {
            name: this.formData.name,
            detail: this.formData.detail,
            time: this.formData.time + ':00',
            personPhone: this.formData.personPhone,
            personName: this.formData.personName,
          },
        })
        if (res.code === '0') {
          this.showForm = false
          this.resetForm()
          uni.showToast({
            title: '添加成功',
            icon: 'success',
          })
          // 刷新列表
          this.getList()
        }
      } catch (error) {
        uni.showToast({
          title: '网络错误',
          icon: 'none',
        })
      }
    },
    onTimeConfirm(e) {
      const date = new Date(e)
      const year = date.getFullYear()
      const month = String(date.getMonth() + 1).padStart(2, '0')
      const day = String(date.getDate()).padStart(2, '0')
      const hours = String(date.getHours()).padStart(2, '0')
      const minutes = String(date.getMinutes()).padStart(2, '0')
      this.formData.time = `${year}-${month}-${day} ${hours}:${minutes}`
      this.showTimePicker = false
    },
    resetForm() {
      this.formData = {
        name: '',
        detail: '',
        time: '',
      }
    },
    showDetail(item) {
      this.currentDetail = { ...item }
      this.showDetailPopup = true
    },
       makePhoneCall(phone) {
      uni.makePhoneCall({
        phoneNumber: phone,
        success: (res) => {
          console.log('拨打电话成功')
        },
        fail: (err) => {
          console.log('拨打电话失败', err)
        },
      })
    },
  },
}
</script>

<style lang="scss" scoped>
.todo-container {
  min-height: 100vh;
  background-color: #f5f5f5;
  padding: 20rpx;
  position: relative;

  .todo-list {
    .todo-item {
      background-color: #fff;
      border-radius: 12rpx;
      padding: 30rpx;
      margin-bottom: 20rpx;
      display: flex;
      justify-content: space-between;
      align-items: center;
      box-shadow: 0 2rpx 10rpx rgba(0, 0, 0, 0.05);

      .todo-info {
        .todo-name {
          font-size: 32rpx;
          color: #333;
          margin-bottom: 10rpx;
        }

        .todo-time {
          font-size: 24rpx;
          color: #999;
        }
      }
    }
  }

  .add-btn {
    position: fixed;
    right: 40rpx;
    bottom: 40rpx;
    width: 100rpx;
    height: 100rpx;
    background-color: #66c2ff;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    box-shadow: 0 4rpx 16rpx rgba(0, 0, 0, 0.2);
    z-index: 99;
  }
  .call-icon{ 
    position: fixed;
    right: 150rpx;
    bottom: 40rpx;
    width: 100rpx;
    height: 100rpx;
    background-color: #66c2ff;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    box-shadow: 0 4rpx 16rpx rgba(0, 0, 0, 0.2);
    z-index: 99;
  }

  .form-container {
    padding: 30rpx;

    .form-title {
      font-size: 36rpx;
      font-weight: bold;
      text-align: center;
      margin-bottom: 40rpx;
    }

    .form-btns {
      margin-top: 40rpx;
      padding: 0 20rpx;
    }
  }

  .detail-container {
    padding: 30rpx;

    .detail-header {
      margin-bottom: 30rpx;

      .detail-title {
        font-size: 36rpx;
        font-weight: bold;
        color: #333;
        margin-bottom: 10rpx;
      }

      .detail-time {
        font-size: 24rpx;
        color: #999;
      }
    }

    .detail-content {
      .detail-label {
        font-size: 28rpx;
        color: #666;
        margin-bottom: 10rpx;
      }

      .detail-text {
        font-size: 28rpx;
        color: #333;
        line-height: 1.6;
      }
    }

    .detail-footer {
      margin-top: 40rpx;
      padding: 0 20rpx;
    }
  }
}
</style> 